//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    imgUrls: [
      'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg',
      'http://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg',
      'http://img06.tooopen.com/images/20160818/tooopen_sy_175833047715.jpg'
    ],
    indicatorDots: false,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    imageheight: 'auto',
    nowcity: '定位中...',
    dwcity: ""

  },
  //事件处理函数
  bindViewTap: function() {

  },
  onLoad: function(param) {
    var that = this;
    wx.getSystemInfo({
      success: function(res) {

        that.setData({
          "imageheight": (res.windowWidth * 9 / 16) + "px"
        });
        //   that.setData({ "dmtop": (res.windowWidth * 768 / 1024) + "px" });


      }
    });
    wx.login({
      success: function(res) {

        console.log(param);
        if (param.city != undefined) {
          that.setData({
            nowcity: param.city
          });
        } else {
          wx.getLocation({
            type: 'wgs84',
            success: function(res) {
              var latitude = res.latitude
              var longitude = res.longitude
              var speed = res.speed
              var accuracy = res.accuracy
              console.log(res)
              that.loadCity(longitude, latitude)
            }
          })
        }

      },
      fail: function(res) {},
      complete: function(res) {},
    })

  },
  todetail: function() {
    wx.navigateTo({
      url: '../showdetail/showdetail'
    })
  },
  tocity: function() {
    wx.navigateTo({
      url: '../city/city?dw='
    })
  },
  tomyinfo: function() {
    wx.navigateTo({
      url: '../myinfo/myinfo'
    })
  },
  loadCity: function(longitude, latitude) {
    var page = this
    wx.request({
      url: 'https://api.map.baidu.com/geocoder/v2/?ak=4N0PRohwPMkaZBEZRXXTeHmI&location=' + latitude + ',' + longitude + '&output=json',
      data: {},
      header: {
        'Content-Type': 'application/json'
      },
      success: function(res) {
        // success  
        console.log(res);
        var city = res.data.result.addressComponent.city;

        page.setData({
          nowcity: city,
          dwcity: city
        });
      },
      fail: function() {
        page.setData({
          currentCity: "获取定位失败"
        });
      },

    })
  },
  tenxunload: function() {

  }

})